<?php

class Relationshipcontroller {

	/**
	 * Controller constructor - direct call to false when being embedded via another controller
	 * @param Registry $registry our registry
	 * @param bool $directCall - are we calling it directly via the framework (true), or via another controller (false)
	 */
	public function __construct( Registry $registry, $directCall )
	{
		$this->registry = $registry;
		$this->registry->getObject('template')->buildFromTemplates('relationship/relation_empty.tpl.php');
				
		if (isset($_POST['action']))
		{
			$action = $_POST['action'];
			switch($action)
			{
				case 'create':
					{
						if (isset($_POST['userid']))
						{
							if (isset($_POST['profileid']))
							{
								$userid = $_POST['userid'];
								$profileid = $_POST['profileid'];
								
								$this->createRelationship($userid, $profileid);
							}
						}
					}
					break;
				case 'accept':
					{
						if (isset($_POST['ID']))
						{
							$reqID = $_POST['ID'];
							$this->approveRelationship($reqID); 
						}	
					}
					break;
				case 'reject':
					{
						if (isset($_POST['ID']))
						{
							$reqID = $_POST['ID'];
							$this->rejectRelationship($reqID); 
						}	
					}
					break;
				case 'delete':
					{
						if (isset($_POST['ID']))
						{
							$reqID = $_POST['ID'];
							$this->removeConnection($reqID); 
						}	
					}
					break;
				default:
					break;
			}
		}
		
		unset($_POST['action']);
		unset($_POST['userid']);
		unset($_POST['profileid']);
	}

	private function createRelationship( $userid, $profileid )
	{
		if( $this->registry->getObject('authenticate')->isLoggedIn() )
		{
			$thisuserid = $this->registry->getObject('authenticate')->getUser()->getUserID();
			$thisprofileid = $this->registry->getObject('authenticate')->getUser()->getCurrentProfile()->getProfileID();
			$name = $this->registry->getObject('authenticate')->getUser()->getCurrentProfile()->getName();
			$photo = $this->registry->getObject('authenticate')->getUser()->getCurrentProfile()->getPhoto();
			
			require_once( FRAMEWORK_PATH . 'models/relationship.php');
			$relationship = new Relationship($this->registry, $thisuserid, $name, $photo, $userid, $thisprofileid, $profileid, 1, 0);
			$result = $relationship->getResult();
				
			echo '<result>'.$result.'</result>';
		}
		else
		{
			$this->registry->errorPage('Please login', 'Only logged in members can connect on this site');
			// display an error*/
		}
		
	}

	private function approveRelationship( $reqID )
	{
		if( $this->registry->getObject('authenticate')->isLoggedIn() )
		{
			require_once( FRAMEWORK_PATH . 'models/relationship.php');
			$relationship = new Relationship( $this->registry, 0, "", "", 0, 0, 0, 1, $reqID );
			if( $relationship->getFriendsID() == $this->registry->getObject('authenticate')->getUser()->getUserID() )
			{
				// we can approve this!
				$relationship->approveRelationship();
				$relationship->save();
				echo '<result>1</result>';
				//$this->registry->errorPage( 'Relationship approved', 'Thank you for approving the relationship');
			}
			else
			{
				$this->registry->errorPage('Invalid request', 'You are not authorized to approve that request');
			}
		}
		else
		{
			$this->registry->errorPage('Please login', 'Please login to approve this connection');
		}

	}

	private function rejectRelationship( $reqID )
	{
		if( $this->registry->getObject('authenticate')->isLoggedIn() )
		{
			require_once( FRAMEWORK_PATH . 'models/relationship.php');
			$relationship = new Relationship(  $this->registry, 0, "", "", 0, 0, 0, 1, $reqID  );
			if( $relationship->getFriendsID() == $this->registry->getObject('authenticate')->getUser()->getUserID() )
			{
				// we can reject this!
				$relationship->delete();
				echo '<result>1</result>';
				//$this->registry->errorPage( 'Relationship rejected', 'Thank you for rejecting the relationship');
			}
			else
			{
				$this->registry->errorPage('Invalid request', 'You are not authorized to reject that request');
			}
		}
		else
		{
			$this->registry->errorPage('Please login', 'Please login to reject this connection');
		}
	}
	
	private function removeConnection($connectionID)
	{
	if( $this->registry->getObject('authenticate')->isLoggedIn() )
		{
			require_once( FRAMEWORK_PATH . 'models/relationship.php');
			$relationship = new Relationship(  $this->registry, 0, "", "", 0, 0, 0, 1, $connectionID  );
			if( $relationship->getFriendsID() == $this->registry->getObject('authenticate')->getUser()->getUserID()
				|| $relationship->getUserID() == $this->registry->getObject('authenticate')->getUser()->getUserID())
			{
				// we can reject this!
				$relationship->delete();
				echo '<result>1</result>';
				//$this->registry->errorPage( 'Relationship rejected', 'Thank you for rejecting the relationship');
			}
			else
			{
				$this->registry->errorPage('Invalid request', 'You are not authorized to remove that request');
			}
		}
		else
		{
			$this->registry->errorPage('Please login', 'Please login to reject this connection');
		}
	}



}


?>